<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/3/15
 * Time: 11:02
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class AlbumGoodsModel
 * @package SDXApi\Model
 */
class AlbumTemplateGoodsModel extends Model
{
	/**
	 * @var string
	 */
	protected $tableName = 'album_template_goods';

	/**
	 * Created by Jerry
	 * @param int $templateId
	 * @return mixed
	 */
	public function findAlbumGoodsByAlbumId($templateId = 0)
	{
		$where = array(
			'template_id'	=>	$templateId,
		);

		return $this->where($where)->select();
	}

	/**
	 * 获取第一个商品图片
	 * Created by Jerry
	 * @param int $templateId
	 * @return mixed
	 */
	public function findAlbumGoodsOriginalBySortDesc($templateId = 0)
	{
		$where = array(
			'a.template_id'	=>	$templateId,
		);

		$field = 'g.original_img';

		$data = $this->alias('a')
						->field($field)
						->join('LEFT JOIN __GOODS__ AS g ON a.goods_id = g.goods_id')
						->where($where)
						->getField('g.original_img');
		return $data;
	}


	/**
	 * Created by Jerry
	 * @param int $templateId
	 * @param int $wid
	 * @return mixed
	 */
	public function findAlbumGoodsInfoByAlbumId($templateId = 0, $wid = 8)
	{
		$where = array(
			'a.template_id'	=>	$templateId,
			'gs.wid'		=>	$wid,
			'g.is_delete'	=>	0,
			'g.is_on_sale'	=>	1,
			'g.original_img'=>	array(
				'NEQ',
				''
			),
		);

		$field = 'a.goods_id,a.sort,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,g.promote_user_rank';

		$data = $this->alias('a')
						->field($field)
						->join('LEFT JOIN __GOODS__ AS g ON a.goods_id = g.goods_id')
						->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON a.goods_id = gs.goods_id')
						->where($where)
						->group('a.goods_id')
						->select();

		return $data;
	}

}